package _05多线程实现多人同时上传;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class UploadThread extends Thread{

    private Socket socket ;

    public UploadThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        String fileName = UUID.randomUUID().toString();
        try (
                //创建文件输出流用于下载客户端传入的文件
                FileOutputStream fos =new FileOutputStream("day12\\服务端文件夹\\"+fileName+".jpg");
                //通过socket对象创建输入流读取客户端文件
                InputStream socketInputStream = socket.getInputStream();
                //通过socket对象创建输出流读取客户端文件
                OutputStream socketOutputStream = socket.getOutputStream()
        ){
            byte[] bytes = new byte[1024];
            int read;
            //读取客户端上传文件
            while ((read=socketInputStream.read(bytes))!=-1){
                fos.write(bytes,0,read);
            }
            System.out.println("服务器端下载完成");

            //回写数据
            socketOutputStream.write("文件已下载".getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
